﻿using CBS.APP;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// EditStudentSemesterAmount.xaml 的交互逻辑
    /// </summary>
    public partial class EditStudentSemesterAmount : Window
    {
        private StudentSemesterAmountViewModel viewModel;

        public EditStudentSemesterAmount()
        {
            InitializeComponent();
            viewModel = new StudentSemesterAmountViewModel();
        }

        public EditStudentSemesterAmount(int StudentSemesterAmountId)
        {
            InitializeComponent();

            viewModel = StudentAmountApp.GetFormById(StudentSemesterAmountId);
            tb_StudentName.Text = viewModel.StudentName;
            tb_Semester.Text = viewModel.SemesterName;
            tb_BeforeAmountBalance.Text = viewModel.BeforeSemesterBalance.ToString();
            tb_Payable.Text = viewModel.Payable.ToString();
            tb_Payment.Text = viewModel.Payment.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            decimal beforeAmountBalance = 0;
            decimal payable = 0;
            decimal patmeny = 0;
            bool isEdit = false;

            if (!string.IsNullOrWhiteSpace(tb_BeforeAmountBalance.Text) && decimal.TryParse(tb_BeforeAmountBalance.Text,out beforeAmountBalance)) 
            {
                viewModel.BeforeSemesterBalance = beforeAmountBalance;
                isEdit = true;
            }

            if (!string.IsNullOrWhiteSpace(tb_Payable.Text) && decimal.TryParse(tb_Payable.Text, out payable))
            {
                viewModel.Payable = payable;
                isEdit = true;
            }

            if (!string.IsNullOrWhiteSpace(tb_Payment.Text) && decimal.TryParse(tb_Payment.Text, out patmeny))
            {
                viewModel.Payment = patmeny;
                isEdit = true;
            }

            if (isEdit) 
            {
                if (StudentAmountApp.UpdateStudentSemesterAmount(viewModel)) 
                {
                    this.DialogResult = true;
                }
            }
        }
    }
}
